package cn.xiehang.SoundSight;

import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.OrderViewHolder> {
    private List<Order> ordersList;

    private OnCancelOrderListener orderListener;
    public interface OnCancelOrderListener {
        void onCancelOrder(String orderId); // 可以根据需要传递更多参数
        void onConfirmOrder(String orderId);
        void onEvaluateOrder(String orderId);
    }








    public OrderAdapter(List<Order> ordersList) {
        this.ordersList = ordersList;

    }
    public void setCancelOrderListener(OnCancelOrderListener listener) {
        orderListener = listener;
    }


    @NonNull
    @Override
    public OrderAdapter.OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_order, parent, false);
        return new OrderAdapter.OrderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull OrderAdapter.OrderViewHolder holder, int position) {
        Order order = ordersList.get(position);

        // 绑定数据
        holder.tvOrderID.setText("订单ID:"+order.getOrderId());
        holder.tvOrderCreateTime.setText("创建时间:"+order.getOrderCreateTime());
        holder.tvOrderStatus.setText("订单状态："+order.getOrderStatus());
        holder.tvOrderTotalPrice.setText("总额:"+String.format("¥%.2f", order.getTotalPrice()));
        holder.tvOrderRate.setText("评分:\n"+order.getRating());


        // 使用Glide加载图片
        Glide.with(holder.itemView.getContext())
                .load(order.getProductImageUrl())
                .placeholder(R.drawable.shopping_mall_iamge)
                .into(holder.ivOrder);

        //通过读取状态设置按钮的可见性
        switch (order.getOrderStatus()) {
            case "待支付":
                holder.btnPayOrder.setVisibility(View.VISIBLE);
                holder.btnCancelOrder.setVisibility(View.VISIBLE);
                holder.btnConfirmOrder.setVisibility(View.GONE);
                holder.btnEvaluateOrder.setVisibility(View.GONE);
                holder.tvOrderRate.setVisibility(View.GONE);
                break;
            case "待收货":
                holder.btnPayOrder.setVisibility(View.GONE);
                holder.btnCancelOrder.setVisibility(View.GONE);
                holder.btnConfirmOrder.setVisibility(View.VISIBLE);
                holder.btnEvaluateOrder.setVisibility(View.GONE);
                holder.tvOrderRate.setVisibility(View.GONE);
                break;
            case "待评价":
                holder.btnPayOrder.setVisibility(View.GONE);
                holder.btnCancelOrder.setVisibility(View.GONE);
                holder.btnConfirmOrder.setVisibility(View.GONE);
                holder.btnEvaluateOrder.setVisibility(View.VISIBLE);
                holder.tvOrderRate.setVisibility(View.GONE);
                break;
            case "已完成":
                holder.btnPayOrder.setVisibility(View.GONE);
                holder.btnCancelOrder.setVisibility(View.GONE);
                holder.btnConfirmOrder.setVisibility(View.GONE);
                holder.btnEvaluateOrder.setVisibility(View.GONE);
                holder.tvOrderRate.setVisibility(View.VISIBLE);
                break;
        }

        // 取消订单按钮点击事件
        holder.btnCancelOrder.setOnClickListener(v -> {
            new AlertDialog.Builder(holder.itemView.getContext())
                    .setTitle("取消订单")
                    .setMessage("确定要取消该订单吗？")
                    .setPositiveButton("确定", (dialog, which) -> {
                        //取消订单逻辑
                        String orderId = order.getOrderId();
                        orderListener.onCancelOrder(orderId);
                    })
                    .setNegativeButton("取消", null)
                    .show();
        });
        // 支付按钮点击事件
        holder.btnPayOrder.setOnClickListener(v -> {
            //支付逻辑
        });
        // 确认收货按钮点击事件
        holder.btnConfirmOrder.setOnClickListener(v -> {
            //确认收货逻辑
            new AlertDialog.Builder(holder.itemView.getContext())
                    .setTitle("确认收货")
                    .setMessage("确定已经收到货物吗？")
                    .setPositiveButton("确定", (dialog, which) -> {
                        //确认收货逻辑
                        String orderId = order.getOrderId();
                        orderListener.onConfirmOrder(orderId);
                    })
                    .setNegativeButton("取消", null)
                    .show();
        });
        // 评价订单按钮点击事件
        holder.btnEvaluateOrder.setOnClickListener(v -> {

            orderListener.onEvaluateOrder(order.getOrderId());

        });

    }

    @Override
    public int getItemCount() {
        return ordersList.size();
    }



    static class OrderViewHolder extends RecyclerView.ViewHolder {

        ImageView ivOrder;//订单图片
        TextView tvOrderID;//订单ID
        TextView tvOrderCreateTime;//订单创建时间
        TextView tvOrderStatus;//订单状态
        TextView tvOrderTotalPrice;//订单总价
        TextView tvOrderRate;//订单评分
        Button btnCancelOrder;//取消订单
        Button btnPayOrder;//支付
        Button btnConfirmOrder;//确认收货
        Button btnEvaluateOrder;//评价订单


        public OrderViewHolder(@NonNull View itemView) {
            super(itemView);
            ivOrder = itemView.findViewById(R.id.iv_Order_Page);
            tvOrderID = itemView.findViewById(R.id.tv_order_id);
            tvOrderCreateTime = itemView.findViewById(R.id.tv_order_create_time);
            tvOrderStatus = itemView.findViewById(R.id.tv_order_status);
            tvOrderTotalPrice = itemView.findViewById(R.id.tv_order_total_price);
            btnCancelOrder = itemView.findViewById(R.id.btn_cancel_order);
            btnPayOrder = itemView.findViewById(R.id.btn_pay_order);
            btnConfirmOrder = itemView.findViewById(R.id.btn_confirm_receipt);
            btnEvaluateOrder = itemView.findViewById(R.id.evaluate_order);
            tvOrderRate = itemView.findViewById(R.id.tv_order_rating);
        }
    }


    //更新存储数据的数量

}